package com.youxin.foundation.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * Base64工具类,使用java自带
 * @author yingde.cao
 *
 */
public class Base64Utils {
	/**
	 * 编码
	 * @param source 原字符串
	 * @return String
	 * @throws UnsupportedEncodingException
	 */
	public static String encode(String source) throws UnsupportedEncodingException
	{
		if(source==null)
		{
			return "";
		}
		byte[] sourceBytes=source.getBytes(BasicConsts.DEFAULT_ENCODE_NAME);
		String result=Base64.getEncoder().encodeToString(sourceBytes);		
		return result;
	}
	/**
	 * 解码
	 * @param source base64编码字符串
	 * @return String
	 * @throws UnsupportedEncodingException
	 */
	public static String decode(String source) throws UnsupportedEncodingException
	{
		if(source==null){
			return "";
		}
		byte[] resultBytes=Base64.getDecoder().decode(source);
		String result=new String(resultBytes, BasicConsts.DEFAULT_ENCODE_NAME);
		return result;
	}
	
}
